package com.fitnessapp.activities;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fitnessapp.database.DatabaseHelper;
import com.fitnessapp.main.FitnessApplication;
import com.fitnessapp.main.IntentExtras;
import com.fitnessapp.main.R;
import com.fitnessapp.main.R.id;
import com.fitnessapp.main.R.layout;
import com.fitnessapp.user.Routine;
import com.fitnessapp.user.User;
import com.fitnessapp.utility.IndexedOnClickListener;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MyRoutinesActivity extends FitnessAppActivity {
	
	private ListView list;
	TextView routineNameInput;
	Spinner routineGoalInput;
	Dialog routineDialog;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setupUI();

	}
	
	private void setupUI(){
		setContentView(R.layout.routines_layout);
		
		list = (ListView)findViewById(R.id.myRoutineList);
		
		updateRoutineList();
		
		
	}
	
	/**
	 * Method called when user clicks create routine button
	 * @param v
	 */
	public void createRoutine(View v){
		
		routineDialog = new Dialog(MyRoutinesActivity.this);
		routineDialog.setContentView(R.layout.create_routine_layout);
		routineDialog.setTitle("Create New Routine");
		
		routineNameInput = (TextView)routineDialog.findViewById(R.id.txtRoutineName);
		routineGoalInput = (Spinner)routineDialog.findViewById(R.id.spinRoutineGoals);
		
		Button createRoutineBtn = (Button)routineDialog.findViewById(R.id.btnCreateRoutine);
		createRoutineBtn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				
				String routineName = routineNameInput.getText().toString();
				String routineGoal = routineGoalInput.getSelectedItem().toString();
				
				if( routineName != null && !routineName.equals("") &&
					routineGoal != null && !routineGoal.equals("") )
				{
					//create the new routine
					Routine routine = new Routine(routineName, routineGoal);
					routine.save(((FitnessApplication)getApplication()).getDatabseWriter());
					User user = ((FitnessApplication)getApplication()).getUser();
					user.addRoutine(routine);
					updateRoutineList();
					
				}else{
					//display error message to user
				}
					
				routineDialog.dismiss();
				routineDialog = null;
				
				
			}
			
		});
		
		routineDialog.show();
		
	}
	
	public void searchRoutines(View v){
		
	}
	
	
	private void updateRoutineList(){
		User user = ((FitnessApplication)getApplication()).getUser();
		
		List<Map<String,String>> routines = user.getRoutineData();				
		
		Log.d("MyRoutineActivity.java", "loading routines " + routines.size());
		
		
		
		SimpleAdapter adapter = new SimpleAdapter(this, routines,
				R.layout.routine_list_item, new String[]{DatabaseHelper.ROUTINE_TABLE_NAME, DatabaseHelper.USER_TABLE_CURRENT_ROUTINE},
				new int[]{R.id.txtRoutineName, R.id.currentRoutineIndicator});
		
		adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
			
			public boolean setViewValue(View view, Object data,
					String text) {
				if( view.getId() == R.id.currentRoutineIndicator){
					RatingBar rating = (RatingBar)view;
					rating.setOnTouchListener(new OnTouchListener() {
						
						public boolean onTouch(View v, MotionEvent event) {
							if( event.getAction() == MotionEvent.ACTION_UP){
								View parent = (View)v.getParent();
								TextView routineName = (TextView)parent.findViewById(R.id.txtRoutineName);
								
								User user = ((FitnessApplication)getApplication()).getUser();
								user.setCurrentRoutine(routineName.getText().toString());
								
								((RatingBar)v).setRating(1.0f);
								
								updateRoutineList();
							}
							return true;
							
						}
					});
					if(text.equals("true")){						
						rating.setRating(1.0f);
					}else{
						rating.setRating(0.0f);
					}
				}else if( view.getId() == R.id.txtRoutineName){
					TextView textView = (TextView)view;
					textView.setText(text);
				}
				return true;
			}
		});
		
		list.setAdapter(adapter);
		
		//find the current routine that is set and mark it		
	//	RatingBar current = (RatingBar)list.getChildAt(user.getCurrentRoutineIndex()).findViewById(R.id.currentRoutineIndicator);
		//current.setRating(1.0f);
		
		//set click listener
		list.setOnItemClickListener(new RoutineClickListener());
	//	int children = list.getCount();
		
		//set the click listeners for each the remove button and the current
		//routine indicator
//			for(int i = 0; i < children; i++){
//				View v = (View) list.getChildAt(i);
//				
//				RatingBar current = (RatingBar)v.findViewById(R.id.currentRoutineIndicator);
//				ImageButton remove = (ImageButton)v.findViewById(R.id.btnRemoveRoutine);
//				
//				//set the listeners for child objects
//				current.setOnClickListener(new CurrentIndicatorClickListener(i));				
//				remove.setOnClickListener(new RemoveRoutineClickListener(i));
//			}
			
		
	}
	
	public void removeRoutineClick(View v){
		View parent = (View)v.getParent();
		TextView routineName = (TextView)parent.findViewById(R.id.txtRoutineName);
		
		User user = ((FitnessApplication)getApplication()).getUser();
		user.removeRoutine(routineName.getText().toString(),  ((FitnessApplication)getApplication()).getDatabseWriter());	
		
		updateRoutineList();
	}
	
	
	
	private class RoutineClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			TextView routineName = (TextView)parent.findViewById(R.id.txtRoutineName);
			
			Intent intent = new Intent(MyRoutinesActivity.this, RoutineActivity.class);
			intent.putExtra(IntentExtras.ROUTINE_INDEX, routineName.getText().toString());
			startActivity(intent);
		}		
	}
	
	

}
